#ifndef __CGI_PARAM_H__
#define __CGI_PARAM_H__

#include <sstream>
#include <string>
#include <tr1/unordered_map>

/**
 * A collection of CGI parameters.
 */
class CgiParam {
public:
  /**
   * Parse out the CGI parameters from the given query string.
   */
  CgiParam(const std::string& queryString);

  /**
   * Get parameters defined in the "QUERY_STRING" environment variable.
   */
  static CgiParam fromEnv();
  
  /**
   * Was a parameter with the given name defined?
   */
  bool has(const std::string& name);

  /**
   * Get one value for the named parameter,
   * returning defaultValue if none was provided.
   */
  template <typename T>
    T getOne(const std::string& name, const T& defaultValue) {
    if (params.count(name)) {
      std::istringstream input(params.find(name)->second);
      T output;
      input >> output;
      return output;
    } else {
      return defaultValue;
    }
  }
private:
  std::tr1::unordered_multimap<std::string, std::string> params;
};

#endif
